package s;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import s.ProxyDemo.UserService;

public class ProxyDemo
{
	public static void main(String[] args)
	{
		UserService us = (UserService)Proxy.newProxyInstance(UserService.class.getClassLoader(), 
				new Class<?>[]{UserService.class}, new MyInvocationHandler(new UserServiceImpl()));
		System.out.println(us.getName());
	}
	
	interface UserService
	{
		String getName();
		
		int getAge();
	}
	
	static class UserServiceImpl implements UserService
	{

		@Override
		public String getName()
		{
			return "ytf";
		}

		@Override
		public int getAge()
		{
			return 20;
		}
		
	}
	
	static class MyInvocationHandler implements InvocationHandler
	{
		
		private Object mTarget;
		
		public MyInvocationHandler()
		{
			super();
		}
		
		public MyInvocationHandler(Object mTarget)
		{
			super();
			this.mTarget = mTarget;
		}

		@Override
		public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
		{
			Object obj = method.invoke(mTarget, args);
			return obj;
		}
		
	}
}
